﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web.Mvc;
using KMF.Common;
using KMF.DAL;
using KMF.WebUI.Areas.KMFadmin.Attributes;
using KMF.WebUI.Areas.KMFadmin.Models;

namespace KMF.WebUI.Areas.KMFadmin.Controllers
{
    [AdminLoggedAuthorize]
    public class AdminGiftController : Controller
    {
        //
        // GET: /Gift/

        public ActionResult GiftList(int page = 1)
        {
            var itemsPerPage = int.Parse(ConfigurationManager.AppSettings["DefaultItemsPerPage"]);
            var totalPage = 0;
            var totalRecord = 0;
            var model = GiftDb.GetAllGift(page, itemsPerPage, ref totalPage, ref totalRecord) ?? new List<ViewGiftPagingDto>();

            ViewBag.PageInfo = new PageInfo { CurrentPage = page, PageSize = itemsPerPage, TotalPages = totalPage };
            ViewBag.TotalRecord = totalRecord;
            return View(model);
        }

        public ActionResult AddGift()
        {
            ViewBag.GiftTypeList = GiftTypeDb.GetAll() ?? new List<GiftTypeDto>();
            ViewBag.GiftLimitationList = GiftLimitationDb.GetAll() ?? new List<GiftLimitationDto>();
            return View();
        }

        [HttpPost]
        public ActionResult AddGift(GiftDto model, string giftProductIdList)
        {
            model.CreatedDate = DateTime.Now;
            model.LimitationTimes = model.LimitationTimes <= 0 ? 0 : model.LimitationTimes;

            var idResult = GiftDb.AddGift(model);
            GiftDb.AddProductIsGift(idResult, giftProductIdList);
            return RedirectToAction("GiftList", "AdminGift");
        }       

        public ActionResult EditGift(int id)
        {
            ViewBag.GiftTypeList = GiftTypeDb.GetAll() ?? new List<GiftTypeDto>();
            ViewBag.GiftLimitationList = GiftLimitationDb.GetAll() ?? new List<GiftLimitationDto>();

            var giftProductIdList = "";
            var giftProductModel = GiftDb.GetProductIsGift(id) ?? new List<GiftProductDto>();
            if(giftProductModel.Any())
            {
                giftProductIdList = giftProductModel.Aggregate(giftProductIdList, (current, item) => current + (item.ProductId + ","));
                giftProductIdList = giftProductIdList.Substring(0, giftProductIdList.Length - 1);
            }
            ViewBag.GiftProductIdList = giftProductIdList;

            var model = GiftDb.GetById(id) ?? new GiftDto();
            return View(model);
        }

        [HttpPost]
        public ActionResult EditGift(GiftDto model, string giftProductIdList)
        {
            model.LimitationTimes = model.LimitationTimes <= 0 ? 0 : model.LimitationTimes;
            GiftDb.UpdateGift(model);
            return RedirectToAction("GiftList", "AdminGift");
        }

        public JsonResult DeleteGift(int giftId)
        {
            var result = GiftDb.DeleteGiftProductByGiftId(giftId);
            return Json(result > 0, JsonRequestBehavior.AllowGet);
        }
    }
}
